package cn.ecnu.controller.forum;

import cn.ecnu.common.utils.R;
import cn.ecnu.service.forum.LikeService;
import cn.ecnu.vo.forum.LikeVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/circle/like")
public class LikeController {

    @Resource
    private LikeService likeService;

    // 给一个Post点赞
    @PostMapping("/likePost")
    public R likeAnPost(@RequestBody LikeVo likeVo) {
        try {
            likeService.likeAnType(likeVo.getUserId(), likeVo.getTypeId(), 0);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    // 给一个Comment点赞
    @PostMapping("/likeComment")
    public R likeAnComment(@RequestBody LikeVo likeVo) {
        try {
            likeService.likeAnType(likeVo.getUserId(), likeVo.getTypeId(), 1);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    // 取消Post点赞
    @PostMapping("/dislikePost")
    public R dislikeAnPost(@RequestBody LikeVo likeVo) {
        try {
            likeService.dislikeAnType(likeVo.getUserId(), likeVo.getTypeId(), 0);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    // 取消一个Comment点赞
    @PostMapping("/dislikeComment")
    public R dislikeAnComment(@RequestBody LikeVo likeVo) {
        try {
            likeService.dislikeAnType(likeVo.getUserId(), likeVo.getTypeId(), 1);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    // 获取一个帖子的点赞用户
    @GetMapping("/getLikeUserOfPost/{postId}")
    public R getLikeUserOfPost(@PathVariable("postId") Integer postId) {
        try {
            return R.ok().put("data", likeService.getLikeUserOfType(postId, 0));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    // 获取一个评论的点赞用户
    @GetMapping("/getLikeUserOfComment/{commentId}")
    public R getLikeUserOfComment(@PathVariable("commentId") Integer commentId) {
        try {
            return R.ok().put("data", likeService.getLikeUserOfType(commentId, 1));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    @PostMapping("/isLikePost")
    public R isLikePost(@RequestBody LikeVo likeVo) {
        try {
            return R.ok().put("data", likeService.isLikeType(likeVo.getUserId(), likeVo.getTypeId(), 0));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    @PostMapping("/isLikeComment")
    public R isLikeComment(@RequestBody LikeVo likeVo) {
        try {
            return R.ok().put("data", likeService.isLikeType(likeVo.getUserId(), likeVo.getTypeId(), 1));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    // 获取一个用户点赞的帖子集合
    @GetMapping("/getPostOfUserLike/{userId}")
    public R getPostOfUserLike(@PathVariable("userId") Integer userId) {
        try {
            return R.ok().put("data", likeService.getLikePostOfUser(userId));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }




}
